package com.bipa.controller;

import com.bipa.bean.User;
import com.bipa.service.OrderService;
import com.bipa.util.PagerModel;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class OrderController {

    @Autowired
    OrderService orderService;

    @RequestMapping("/saveOrder")
    public ModelAndView saveOrder(@SessionAttribute(value = "session_user",required = false) User user, String totalMoney,String aId_buyNum) throws Exception{

        ModelAndView mv = new ModelAndView();
        //判断用户是否为空，为空说明用户暂未登录
        if(user == null) {
            //跳转至登录页面
            mv.addObject("msg", "您尚未登录，请登录后在进行该操作！");
            mv.setViewName("front/login");
        }else{
            orderService.saveOrder(totalMoney,aId_buyNum,user.getId());
            //展示当前用户的订单信息
            mv.setViewName("redirect:./front/showOrder");
        }
        return mv;

    }

    @RequestMapping("/showOrder")
    public ModelAndView showOrder(@SessionAttribute(value = "session_user",required = false) User user){
        ModelAndView mv = new ModelAndView();
        if (user == null){
            mv.addObject("msg", "您尚未登录，请登录后在进行该操作！");
            mv.setViewName("front/login");
        }else {
            mv.addObject("orders",orderService.getOrdersByUserId(user.getId()));
            mv.addObject("highLight","ordereby");
            mv.setViewName("front/order");
        }
        return mv;
    }

    @RequestMapping("/mOrderList")
    public ModelAndView toOrderList(@RequestParam(value = "payStatus",defaultValue = "")String payStatus,@RequestParam(value = "ordeCode",defaultValue = "")String ordeCode){
        ModelAndView mv = new ModelAndView();
        PagerModel pagerModel = new PagerModel();
        pagerModel.setPageSize(10);
        pagerModel.setTotalNum(orderService.getAll(payStatus,ordeCode));
        //System.out.println(pagerModel.getTotalNum());
        mv.addObject("pageModel",pagerModel);
        mv.addObject("highLight", "orderty");
        mv.addObject("orderList",orderService.findAllOrders(pagerModel,payStatus,ordeCode));
        mv.setViewName("back/order_list");
        return mv;
    }

    @RequestMapping("checkOrder")
    public ModelAndView sendOrder(@Param("id")Integer id,@Param("status")Integer status){
        ModelAndView mv = new ModelAndView();
        orderService.checkOrder(id,status);
        mv.setViewName("redirect:./mOrderList");
        return mv;
    }
}
